package com.itelite.handler;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.Map;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 * @create 07-06-2021-13:33
 **/

public class ValidateHandler {
    public static final String VALIDATE_SUCCESS="校验通过";
    public static String catchErrors(BindingResult errors){
        StringBuilder str = new StringBuilder();
        errors.getAllErrors().forEach(error->{
            FieldError fieldError = (FieldError) error;
            str.append(fieldError.getField()).append(":").append(error.getDefaultMessage());
            if(errors.hasErrors()) str.append(",");
        });
        return str.toString();
    }
    public static String catchErrors(Map<String,Object> params, String... str){
        for (String s : str) {
            if(params.get(s)==null) return "参数："+s+"为空";
        }
        return VALIDATE_SUCCESS;
    }
}
